package controller;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Calendar;

/**
 * 12-01-2010
 * 
 * This class is the controller to get all the content on the calendar.
 * 
 * @author Dave Niewenhuijzen
 */
public class CalendarContentController implements MouseListener {
	private model.CalendarModel calendarModel;
	private AppointmentController appointmentController;
	
	/**
	 * Default constructor.
	 * 
	 * @param setCalendarModel
	 * @param setAppointmentModel
	 */
	public CalendarContentController(model.CalendarModel setCalendarModel, model.AppointmentModel setAppointmentModel, AppointmentController setAppointmentController) {
		calendarModel = setCalendarModel;
		appointmentController = setAppointmentController;
	}

	/**
	 * This will look if someone double clicks if it in the week or month view
	 * 
	 * @param e
	 */
	public void mouseClicked(MouseEvent e) {
		// Only execute if we double click with the left mouse button
		if(e.getClickCount() == 2 && e.getButton() == 1) {
			if(calendarModel.getCalendarViewMode() == model.CalendarViewMode.month) {
				doubleClickMonth(e);
			} else {
				doubleClickWeek(e);
			}
		}
	}

	/**
	 * When double clicked it will look if you clicked a appointment or clicked on a empty space
	 * If the appointment is clicked it will open the appointment
	 * If you clicked on a empty space, the create new appointment window will open.
	 * 
	 * @param e
	 */
	private void doubleClickWeek(MouseEvent e) {
		boolean editAppointment = false;
		int yPosition = e.getY() + calendarModel.getScrollValue();

		int total = calendarModel.getAppointmentData().size();
		model.AppointmentDataModel appointmentData = null;

		// Loop through all appointments while total is not equal to 0 and editAppointment is equal to false
		while (total != 0 && editAppointment == false) {
			appointmentData = calendarModel.getAppointmentData().get(--total);

			if((yPosition >= appointmentData.getBounds().getY()) && (yPosition <= appointmentData.getBounds().getHeight()) && (e.getX() >= appointmentData.getBounds().getX()) && (e.getX() <= appointmentData.getBounds().getWidth())) {
				// If for some reason the appointment id is not greater then 0 we skip it.
				if(appointmentData.getId() > 0) {
					// We found an appointment in the area we clicked so editAppointment is changed to true.
					editAppointment = true;

					try {
						appointmentController.editAppointment(appointmentData.getId());
						appointmentController.setInputTitle(appointmentData.getTitle());
						appointmentController.setInputStartDateTime(appointmentData.getStartDate().get(Calendar.YEAR), appointmentData.getStartDate().get(Calendar.MONTH), appointmentData.getStartDate().get(Calendar.DAY_OF_MONTH), appointmentData.getStartDate().get(Calendar.HOUR_OF_DAY), appointmentData.getStartDate().get(Calendar.MINUTE));
						appointmentController.setInputEndDateTime(appointmentData.getEndDate().get(Calendar.YEAR), appointmentData.getEndDate().get(Calendar.MONTH), appointmentData.getEndDate().get(Calendar.DAY_OF_MONTH), appointmentData.getEndDate().get(Calendar.HOUR_OF_DAY), appointmentData.getEndDate().get(Calendar.MINUTE));
						appointmentController.setInputLocation(appointmentData.getLocation());
						appointmentController.setInputContactPerson(appointmentData.getContact());
						appointmentController.setInputDescription(appointmentData.getDescription());
						appointmentController.setInputCategory(appointmentData.getCategory());
					} catch(Exception x) {
						// Error but we don't use it, we just skip this appointment
					}
				}
			}
		}

		// If editAppointment is equal to false we add a new appointment.
		if(editAppointment == false) {
			Calendar selectedDate = calendarModel.getPositionDateTimeWeekView(e.getX(), yPosition);  

			appointmentController.newAppointment();
			selectedDate.add(Calendar.MINUTE, -15);

			appointmentController.setInputStartDateTime(selectedDate.get(Calendar.YEAR), selectedDate.get(Calendar.MONTH), selectedDate.get(Calendar.DAY_OF_MONTH), selectedDate.get(Calendar.HOUR_OF_DAY), selectedDate.get(Calendar.MINUTE));
			appointmentController.setInputEndDateTime(selectedDate.get(Calendar.YEAR), selectedDate.get(Calendar.MONTH), selectedDate.get(Calendar.DAY_OF_MONTH), selectedDate.get(Calendar.HOUR_OF_DAY) + 1, selectedDate.get(Calendar.MINUTE));
		}
	}

	/**
	 * This will open the week view if there is double clicked in the month view.
	 * 
	 * @param e Is the mouse event.
	 */
	private void doubleClickMonth(MouseEvent e) {
		calendarModel.setSelectedDate(calendarModel.getPositionDateTimeMonthView(e.getX(), e.getY()));
		calendarModel.setCalendarViewMode(model.CalendarViewMode.week);
	}

	/**
	 * This must be here for the MouseListener to avoid errors.
	 * 
	 * @param e Is the mouse event.
	 */
	public void mouseEntered(MouseEvent e) {

	}

	/**
	 * This must be here for the MouseListener to avoid errors.
	 * 
	 * @param e Is the mouse event.
	 */
	public void mouseExited(MouseEvent e) {

	}

	/**
	 * This must be here for the MouseListener to avoid errors.
	 * 
	 * @param e Is the mouse event.
	 */
	public void mousePressed(MouseEvent e) {

	}

	/**
	 * This must be here for the MouseListener to avoid errors.
	 * 
	 * @param e Is the mouse event.
	 */
	public void mouseReleased(MouseEvent e) {

	}
}